package com.dog.modules.message.web;

import com.dog.framework.core.model.ResponseResult;
import com.dog.framework.core.utils.ExceptionUtils;
import com.dog.framework.log.annotation.OperateLog;
import com.dog.framework.log.enums.BusinessType;
import com.dog.modules.message.convert.SmsPlatformConvert;
import com.dog.modules.message.domain.entity.SmsPlatform;
import com.dog.modules.message.domain.vo.SmsPlatformVO;
import com.dog.modules.message.domain.vo.SmsSendVO;
import com.dog.modules.message.service.SmsPlatformService;
import com.dog.modules.message.sms.SmsContext;
import com.dog.modules.message.sms.config.SmsConfig;
import com.dog.modules.message.sms.service.SmsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信平台
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("/message/sms/platform")
@Tag(name = "短信平台")
@AllArgsConstructor
public class SmsPlatformController {

    private final SmsPlatformService smsPlatformService;
    private final SmsService smsService;


    @GetMapping("{id}")
    @Operation(summary = "信息")
    public ResponseResult<SmsPlatformVO> get(@PathVariable("id") Long id) {
        SmsPlatform entity = smsPlatformService.getById(id);
        return ResponseResult.success(SmsPlatformConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(businessType = BusinessType.INSERT)
    public ResponseResult<String> save(@RequestBody SmsPlatformVO vo) {
        smsPlatformService.save(vo);
        return ResponseResult.success();
    }

    @PostMapping("send")
    @Operation(summary = "发送短信")
    @OperateLog(businessType = BusinessType.OTHER)
    public ResponseResult<String> send(@RequestBody SmsSendVO vo) {
        SmsPlatform entity = smsPlatformService.getById(vo.getId());
        SmsConfig config = SmsPlatformConvert.INSTANCE.convert2(entity);

        // 短信参数
        Map<String, String> params = new LinkedHashMap<>();
        if (StringUtils.isNotBlank(vo.getParamValue())) {
            params.put(vo.getParamKey(), vo.getParamValue());
        }

        try {
            // 发送短信
            new SmsContext(config).send(vo.getMobile(), params);
            // 保存日志
            smsService.saveLog(config, vo.getMobile(), params, null);
            return ResponseResult.success();
        } catch (Exception e) {
            // 保存日志
            smsService.saveLog(config, vo.getMobile(), params, e);
            return ResponseResult.fail(ExceptionUtils.getExceptionMessage(e));
        }
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(businessType = BusinessType.UPDATE)
    public ResponseResult<String> update(@RequestBody @Valid SmsPlatformVO vo) {
        smsPlatformService.update(vo);
        return ResponseResult.success();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(businessType = BusinessType.DELETE)
    public ResponseResult<String> delete(@RequestBody List<Long> idList) {
        smsPlatformService.delete(idList);
        return ResponseResult.success();
    }
}